<template>
  <div style="display: inline-block">
    <el-dialog :visible.sync="dialog" :close-on-click-modal="false" :title="title" append-to-body width="500px"
               @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="88px">
        <!-- <el-form-item label="账号" prop="oldPass">
          <el-input v-model="form.userName"  auto-complete="on" style="width: 370px;"/>
        </el-form-item> -->
        <el-form-item label="新密码" prop="newPass">
          <el-input v-model="form.newPass" type="password" auto-complete="on" style="width: 370px;" maxlength="20"/>
         <!-- <el-progress :percentage="passwordPercent" :format="passwordPercentFormat"/> -->
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPass">
          <el-input v-model="form.confirmPass" type="password" auto-complete="on" maxlength="20" style="width: 370px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import store from '@/store'
  import { updateUserPass } from '@/api/framework/system/user'

  export default {
    data() {
      const validatePassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'))
        } else {
          //8-20位包含字符、数字和特殊字符
          var ls = 0
          if (this.form.newPass !== '') {
            if (this.form.newPass.match(/([a-z])+/)) {
              ls++
            }
            if (this.form.newPass.match(/([0-9])+/)) {
              ls++
            }
            if (this.form.newPass.match(/([A-Z])+/)) {
              ls++
            }
            if (this.form.newPass.match(/([\W])+/) && !this.form.newPass.match(/(![\u4E00-\u9FA5])+/)) {
              ls++
            }
            if (this.form.newPass.length < 8 || this.form.newPass.length > 20) {
              callback(new Error('要求8-20位字符'))
              ls = 0
            }
            if (this.form.newPass.match(/([\u4E00-\u9FA5])+/)) {
              callback(new Error('不能包含中文字符'))
              ls = 0
            }
            switch (ls) {
              case 0:
                this.passwordPercent = 0
                callback(new Error('至少满足(数字、小写字母、大写字母、特殊字符)三个条件'))
                break
              case 1:
                this.passwordPercent = 33
                callback(new Error('至少满足(数字、小写字母、大写字母、特殊字符)三个条件'))
                break
              case 2:
                this.passwordPercent = 66
                callback(new Error('数至少满足(数字、小写字母、大写字母、特殊字符)三个条件'))
                break
              case 3:
              case 4:
                this.passwordPercent = 100
                break
              default:
                this.passwordPercent = 0
                break
            }
          }
          callback()
        }
      }
      const confirmPass = (rule, value, callback) => {
        if (value) {
          if (this.form.newPass !== value) {
            callback(new Error('两次输入的密码不一致'))
          } else {
            callback()
          }
        } else {
          callback(new Error('请再次输入密码'))
        }
      }
      return {
        passwordPercent: 0,
        loading: false, dialog: false, title: '修改密码', form: { userName: '', newPass: '', confirmPass: '' },
        rules: {
          newPass: [
            { required: true,  trigger: ['blur', 'change'] }
          ],
          confirmPass: [
            { required: true, validator: confirmPass, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      passwordPercentFormat(percentage) {
        return percentage === 100 ? '符合' : '不符'
      },
      cancel() {
        this.resetForm()
      },
      doSubmit() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.loading = true
            updateUserPass(this.form).then(res => {
              this.resetForm()
              this.$notify({
                title: '密码修改成功!',
                type: 'success',
                duration: 1500
              })
            }).catch(err => {
              this.loading = false
              console.log(err.response.data.message)
            })
          } else {
            return false
          }
        })
      },
      resetForm() {
        this.dialog = false
        this.passwordPercent = 0
        this.$refs['form'].resetFields()
        this.form = { userName: '', newPass: '', confirmPass: '' }
      }
    }
  }
</script>

<style scoped>

</style>
